//配置信息
var sn       = 'SDK-LJP-010-00092';
var pwd      = '6AC296B2F8E0676F3F8FC8E32CB67CDB';
var dbConfig = {
  host     : 'rdsy3g31gwkiheegh3jyd.mysql.rds.aliyuncs.com',
  user     : 'lianying_local',
  password : 'lianyingdai',
  port     : '3306',
  database : 'lianyingdai_local',
}

//mysql
var request = require('request');
var http = require('http');
var querystring = require('querystring');
var mysql      = require('mysql');
var db = mysql.createConnection(dbConfig);

 
 
//开启web服务 
http.createServer(function (request, response) {   
    if(request.url !== '/favicon.ico'){
        var body = '';
        request.on('data', function(data){
            body += data;
        });
        
        request.on('end', function(){
             body = querystring.parse(body);
             var smsData = getData(body.mobile,body.message);
             console.log(smsData);
             post(smsData);
             
        });
        
    }
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.write('<h1>Node.js</h1>');
    response.end();
    
}).listen(8888);


/**
 * get sms data
 */
function getData(mobile, message){ 

    var argv = {};
    argv['sn'] = sn;
    argv['pwd'] = pwd;
    argv['mobile'] = mobile;
    argv['content'] = message;
    argv['ext'] = '';
    argv['stime'] = '';
    argv['rrid'] = '';
    return argv;
}

/**
 * send sms to  server
 */
function post(params){
    var data = {};
    data.url = 'http://sdk2.zucp.net:8060/webservice.asmx/mt';
    data.form = params;
    var res = request.post(data,function(err, httpResponse, body){
        console.log(1);
        //console.log(body);
    });
}


function smsSave(mobile, message){
    db.connect();
    db.query('USE' + dbConfig.database);
    var sql = "insert into lzh_member_sms('phone','message','add_time') values(?,?,?) ";
    var add_time =  Date.parse(new Date())/1000;
    var data = [mobile, message, add_time];
    db.query(sql, data, function(err, result){
        
    });
}


smsSave('18665590815','测试短信添加');





